package chapters15_bingfa.chapters15_1;

/**
 * Created by xinxuchu on 2020/1/25.
 * 竞态条件（race condition）：当多个线程访问和操作同一个对象时，最终执行结果与执行时序有关，可能正确也可能不正确。
 *
 * java 并发章节学习
 */
public class CountThread extends Thread {


    private static int  counter = 0;

    @Override
    public void run(){
        int times = 1000;
        for (int i=0;i<times;i++){
            counter++;
        }
    }

    public static void main(String[] args) throws InterruptedException{
        int num = 1000;
        Thread[] threads = new  Thread[num];
        for (int i = 0; i <num ; i++) {
            threads[i] = new CountThread();
            threads[i].start();
        }

        for (int i = 0; i < num; i++) {

            threads[i].join();
        }
        System.out.println(counter);

    }
}


